package DAO;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

import Main.DBMain;

public class DelClass extends JFrame {
	private JPanel contentPane = new JPanel();
	private JLabel lab_Tip;
	private JTextField classNoField;
	private JButton btn_Del, btn_Exit;
	String getClassNo = null;

	public DelClass() {
		setTitle("删除班级");
		setSize(350, 200);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Tip = new JLabel("请输入要删除的班级：");
		lab_Tip.setBounds(20, 35, 160, 25);
		lab_Tip.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		contentPane.add(lab_Tip);

		classNoField = new JTextField();
		classNoField.setBounds(175, 35, 130, 25);
		/* 添加回车监听，点击删除班级 */
		classNoField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					delClass();
					setVisible(false);
				}
			}
		});
		contentPane.add(classNoField);

		btn_Del = new JButton("删除");
		btn_Del.setBounds(70, 100, 70, 30);
		btn_Del.setFont(new Font("微软雅黑", Font.BOLD, 16));
		/* 添加按钮监听，点击删除班级 */
		btn_Del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delClass();
				setVisible(false);
			}
		});
		contentPane.add(btn_Del);

		btn_Exit = new JButton("取消");
		btn_Exit.setBounds(210, 100, 70, 30);
		btn_Exit.setFont(new Font("微软雅黑", Font.BOLD, 16));
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		contentPane.add(btn_Exit);

		setVisible(true);
	}

	public void delClass() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		getClassNo = classNoField.getText().trim();//获取要删除的班级
		if (!("".equals(getClassNo))) {
			try {
				conn = DBMain.getConnection();
				String sql = "delete from stu_info where classNo=?";//删除语句
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, getClassNo);
				int flag = pstmt.executeUpdate();   //更新，删除该班级
				// 记录删除结果
				if (flag != 0) {
					JOptionPane.showMessageDialog(null, "删除成功！");
				} else {
					JOptionPane.showMessageDialog(null, "班级不存在，删除失败！");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "请输入班级！");
		}
	}
}
